/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package comunications;

import businessLogic.PlayerActions;
import crashManager.CrashManager;
import gameState.Player;
import gameState.Ring;
import gameState.Table;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DEX
 */

public class Ping extends Thread{

        private int assenteDa;
        private Ring ring;
        private PlayerActions pa;
        Logger logger = Logger.getLogger(this.getClass().getSimpleName());


        public Ping(PlayerActions pa){
            this.pa = pa;
            this.ring = Ring.getInstance();
            this.assenteDa = 0;
        }


        public Ping(int assenza, PlayerActions pa) {
            this.pa = pa;
            this.ring = Ring.getInstance();
            this.assenteDa = assenza;
        }


        public void run() {
                if(!ring.amIalone()){
                    Player player = ring.getMyNextPlayer();
                    boolean alive=true;
                    try{
                        Registry registry = LocateRegistry.getRegistry(player.getIp(), player.getPort());
                        ClientComunicateInterface cci = (ClientComunicateInterface)registry.lookup("ClientComunicateImpl");
                        if (cci.areYouALive()){
//                          logger.info("[Ping] Appena pingato "+player.getName());
                            Thread ping = new Ping(pa);
                            ping.sleep(1000);
                            ping.start();
                        }
                    }
                    catch(Exception e){
                        try{
                            logger.info("[Ping] Non riesco a pingare "+player.getName()+" da "+assenteDa+" secondi.");
                            if(assenteDa<1){
                                Thread ping = new Ping(assenteDa+1, pa);
                                ping.sleep(1000);
                                ping.start();
                            }
                            else{
                                CrashManager cm = new CrashManager();
                                cm.solveNext();
                                Thread.sleep(1900);

                                Thread st = new SendTable(pa.getTable(), ring.getMyNextPlayer());
                                logger.info("[Ping] dopo un crash scoperto da Ping, invio a "+ring.getMyNextPlayer().getName()+" un tavolo di versione "+pa.getTable().getVersion());
                                st.start();
                                Thread ping = new Ping(pa);
                                ping.sleep(100);
                                ping.start();

                            }
                        }
                        catch(Exception e2){
                            logger.warning("[Ping] quanlcuno ha fatto macello in PING");
                        }
                    }
                }
        }
}

